#include "GameTile.h"

// Maybe clear the sdl surfaces
GameTile::~GameTile()
{
}


SDL_Surface* GameTile::drawPlayers(SDL_Surface* a_dest, SDL_Surface* a_source, Position a_idArray[], Player * a_players, bool drakonSummoned){
	for(int i = 0; i < NUMBER_OF_PLAYERS; ++i) {

		if(a_players[i].getPosition().equals(m_pos)){
			SDL_Rect image;
			Quadrants quad;
			int tW = TILE_ICON_WIDTH;
			int tH = TILE_ICON_HEIGHT;
			int row = TILE_PLAYER_ROW_START;
			image.w = tW;
			image.h = tH;
		
			image.x = i*tW;
			image.y = row*tH;

			// can make this less redundant, but there won't be more than 4/6 players, and quadrants need to be specific
			/// possibly make an accessor in class Quadrants to compute the player position on the tile
			if(i == 0) { // player number 1
				SDL_Rect dest = {quad.quads[0].getX(),quad.quads[0].getY(),0,0};
				SDL_BlitSurface(a_source, &image, a_dest, &dest);
			}
			else if (i == 1){
				SDL_Rect dest = {quad.quads[2].getX(),quad.quads[2].getY(),0,0};
				SDL_BlitSurface(a_source, &image, a_dest, &dest);
			}
			else if(i == 2){
				SDL_Rect dest = {quad.quads[6].getX(),quad.quads[6].getY(),0,0};
				SDL_BlitSurface(a_source, &image, a_dest, &dest);
			}
			else if(i == 3){
				SDL_Rect dest = {quad.quads[8].getX(),quad.quads[8].getY(),0,0};
				SDL_BlitSurface(a_source, &image, a_dest, &dest);
				
			}
			// WARNING ONLY WORKS with 4 PLAYERS MUST FIX THIS 
		/*	else if(i == 4) {
				image.w = tW*4;
				image.h = tH*4;
				image.x = 0;
				image.y = (row+1)*tH;
				SDL_Rect dest = {quad.quads[0].getX()-25,quad.quads[0].getY()-30,0,0};
				SDL_Rect destRect = {0,0,0,0};

				SDL_Surface * tempCopySurface = SDL_ConvertSurface(a_dest, a_dest->format, SDL_SWSURFACE);
				
				SDL_BlitSurface(a_source,&image,tempCopySurface,&dest);
				SDL_SetAlpha(tempCopySurface,SDL_SRCALPHA,DRAKON_ALPHA);
				SDL_BlitSurface(tempCopySurface,NULL,a_dest,&destRect);
				
				SDL_FreeSurface(tempCopySurface);
			}*/

		
		}
	}
	// DRAW DRAKON IF there
	if(drakonSummoned){
		
		if(a_players[NUMBER_OF_PLAYERS].getPosition().equals(m_pos)){
			SDL_Rect image;
			Quadrants quad;
			int tW = TILE_ICON_WIDTH;
			int tH = TILE_ICON_HEIGHT;
			int row = TILE_PLAYER_ROW_START;

			image.w = tW*4;
			image.h = tH*4;
			image.x = 0;
			image.y = (row+1)*tH;
		// merged Binh and Gindi's code
		//	SDL_Rect dest = {quad.quads[0].getX()-25,quad.quads[0].getY()-30,0,0};
			SDL_Rect dest = {quad.quads[0].getX()-12,quad.quads[0].getY()-15,0,0};
			SDL_Rect destRect = {0,0,0,0};

			SDL_Surface * tempCopySurface = SDL_ConvertSurface(a_dest, a_dest->format, SDL_SWSURFACE);
			
			SDL_BlitSurface(a_source,&image,tempCopySurface,&dest);
			SDL_SetAlpha(tempCopySurface,SDL_SRCALPHA,DRAKON_ALPHA);
			SDL_BlitSurface(tempCopySurface,NULL,a_dest,&destRect);
			
			SDL_FreeSurface(tempCopySurface);
		}
	}
	return a_dest;
}
SDL_Surface* GameTile::drawTile(SDL_Surface * a_buffer, SDL_Surface * a_sourceBG, SDL_Surface * a_sourceTile, Position a_idArray[]){
	return m_tile.getTileSurface(a_buffer,a_sourceBG,a_sourceTile,a_idArray);
}
SDL_Surface* GameTile::getGameTileSurface(SDL_Surface * a_buffer, SDL_Surface * a_sourceBG, SDL_Surface * a_sourceTile, Position a_idArray[], Player * a_players,bool a_drakonExists){
	SDL_Surface * tempSurface = NULL;
	tempSurface = drawTile(a_buffer,a_sourceBG,a_sourceTile,a_idArray);
	tempSurface = drawPlayers(tempSurface, a_sourceTile, a_idArray, a_players,a_drakonExists);

	return tempSurface;
}